<?php

namespace app\common\model;

use think\Db;
use think\Model;

class SettingRegister extends Model
{
    // 表名
    protected $name = 'setting_register';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'status_text'
    ];
    
	CONST STATUS_Y = 1;
	CONST STATUS_N = 0;
	
	public static $statusList = [
		self::STATUS_Y => '正常',
		self::STATUS_N => '禁用'
	];

	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = self::$statusList;
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	
	
	/**
	 * 注册赠送金额
	 * @param $userId 用户
	 * @return bool
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public function sendAmount($userId){
		$config = Db::name('SettingRegister')
			->where(['status' => self::STATUS_Y])
			->find();
		
		if($config) {
			$fundsModel = new FundsRecord();
			
			if($config['balance'] > 0) {
				$userRes = Db::name('user')
					->where(['id' => $userId])
					->setInc('balance', $config['balance']);
				
				$fundsRes = $fundsModel->addRecord($userId, $config['balance'], 0, $fundsModel::ACCOUNT_TYPE_BALANCE, $fundsModel::OPE_TYPE_PLUS, $fundsModel::TYPE_REG_GIFT);
			}
			
			if($config['integral'] > 0) {
				$userRes = Db::name('user')
					->where(['id' => $userId])
					->setInc('integral', $config['integral']);
				
				$fundsRes = $fundsModel->addRecord($userId, $config['integral'], 0, $fundsModel::ACCOUNT_TYPE_INTEGRAL, $fundsModel::OPE_TYPE_PLUS, $fundsModel::TYPE_REG_GIFT);
			}
			
			if($config['voucher'] > 0) {
				$userRes = Db::name('user')
					->where(['id' => $userId])
					->setInc('voucher_balance', $config['voucher']);
				
				$fundsRes = $fundsModel->addRecord($userId, $config['voucher'], 0, $fundsModel::ACCOUNT_TYPE_VOUCHER, $fundsModel::OPE_TYPE_PLUS, $fundsModel::TYPE_REG_GIFT);
			}
			
			if($config['phone'] > 0) {
				$userRes = Db::name('user')
					->where(['id' => $userId])
					->update([
						'phone_balance' => $config['phone'],
						'phone_time' => strtotime('+'.$config['phone_days'].' days')
					]);
				
				$fundsRes = $fundsModel->addRecord($userId, $config['balance'], 0, $fundsModel::ACCOUNT_TYPE_PHONE, $fundsModel::OPE_TYPE_PLUS, $fundsModel::TYPE_REG_GIFT);
			}
		}
		
		return true;
	}
}
